/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package business;

import business.index;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Hatta
 */

public class DaftarIndex {

    private int jumlahIndex = -1;

    public DaftarIndex() {
        emf = Persistence.createEntityManagerFactory("apoltekkPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /**
     * @return the jumlahUser
     */
    public int getJumlahIndex() {

        if (jumlahIndex == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) FROM index as o");
                Number jumlah = (Number) q.getSingleResult();
                jumlahIndex = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahIndex;
    }

    public List<index> sepuluhIndexTerbaru() {
        List<index> index= null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM index as o ORDER BY o.id DESC");
            q.setMaxResults(10);
            index= q.getResultList();

        } catch (javax.persistence.EntityNotFoundException e) {
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return index;
    }

    public index getIBOp (String admin){
        index ib  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM index as o WHERE o.admin = :admin");
            q.setParameter("admin", admin);
            ib = (index) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return ib;
    }

    public index getIndex (Long id){
        index ib  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM index as o WHERE o.id = :id");
            q.setParameter("id", id);
            ib = (index) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return ib;
    }

    public void tambahIndex(index ib) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ib);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void hapusIndex(index ib) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ib);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void editIndex(index ib) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(ib);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

}

